import requests
from bs4 import BeautifulSoup
from prettytable import *
import random
import json
import datetime
import calendar
from datetime import timedelta
import mysql
import time


def get_url(url, params=None, proxies=None):
    user_agent_list = ["Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
                "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
                "Mozilla/5.0 (Windows NT 10.0;) Gecko/20100101 Firefox/61.0",
                "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36",
                "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36",
                "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
                "Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.5; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15",
                ]

    random_agent = random.choice(user_agent_list)
    # print(random_agent)

    header = {
        'User-Agent' : random_agent,
        'Referer' : 'http://fundf10.eastmoney.com/'
    }
    # print(params)
    rsp = requests.get(url, params=params, proxies=proxies, headers=header)
    rsp.encoding = "utf-8"
    rsp.raise_for_status()
    return rsp.text


def get_fund_data(code, start='', end=''):
    record = {'Code': code}
    url = 'http://api.fund.eastmoney.com/f10/lsjz'
    params = {'fundCode': code, 'pageIndex': 1, 'pageSize': 400, 'startDate': start, 'endDate': end, '_': random.random()}
    json_str = get_url(url, params)
    # print(json_str)
    json_obj = json.loads(json_str)

    records = []
    for o in json_obj['Data']['LSJZList']:
        record['Date'] = o['FSRQ']
        record['NetAssetValue'] = o['DWJZ']
        record['ChangePercent'] = o['JZZZL']
        records.append(record.copy())

    return records


# def demo(code, start, end):
#     table = PrettyTable()
#     table.field_names = ['Code', 'Date', 'NAV', 'Change']
#     table.align['Change'] = 'r'
#     records = get_fund_data(code, start, end)
    
    # for record in records:
    #     table.add_row([record['Code'], record['Date'], record['NetAssetValue'], record['ChangePercent']])
    # return table



if __name__ == "__main__":


    records = mysql.getAllFunds("")

    err_num = 0

    for record in records:


        fundId = record['Code']

        updateType = "history"

        try:

            if updateType == "history":
                curYear = datetime.datetime.now().year
                i = 1
                while i <= 5:
                    startDay = datetime.date(curYear,1,1)
                    endDay = datetime.date(curYear,12,31)
                    print("正在抓取基金净值%s：开始时间%s, 结束时间%s" % (fundId, startDay, endDay))

                    records = get_fund_data(fundId, startDay, endDay)
                    print("此次更新净值数量：%s" % (len(records)))
                    mysql.updateFundValue(fundId, records)
                    
                    curYear -= 1
                    i += 1
                    time.sleep(1)
            else:
                curYear = datetime.datetime.now().year
                i = 1
                while i <= 1:
                    startDay = datetime.date(curYear,1,1)
                    endDay = datetime.date(curYear,12,31)
                    print("正在抓取基金净值%s：开始时间%s, 结束时间%s" % (fundId, startDay, endDay))

                    records = get_fund_data(fundId, startDay, endDay)
                    mysql.updateCurYearFundValue(fundId, records)
                    
                    curYear -= 1
                    i += 1
                    time.sleep(1)

        except Exception as e:
            print(e)
            err_num += 1
            print("共计失败次数：%s" % (err_num))





        # print(demo('004997', firstDay, lastDay))
# http://fund.eastmoney.com/f10/F10DataApi.aspx?type=lsjz&code=050026&page=1&sdate=2020-02-01&edate=2020-06-01&per=20

